package com.semidata.trp.report.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;

import com.semidata.rtd.core.http.server.DefaultDispatcher;
import com.semidata.rtd.core.http.server.HttpDispatchHandler;
import com.semidata.rtd.core.http.server.UrlMatcher;
import com.semidata.trp.report.config.Config;

public class ReportHttpDispatcher extends DefaultDispatcher {

	public ReportHttpDispatcher() {
		super();

		Config config = Config.getConfig();
		String urlRecomputer = config.getRecomputeUrl();
		String urlReport = config.getReportUrl();
		String urlTest = "/test/";

		if (!urlRecomputer.endsWith("/")) {
			urlRecomputer += "/";
		}

		if (!urlReport.endsWith("/")) {
			urlReport += "/";
		}

		addUrlMatcher(new UrlMatcher(urlRecomputer) {

			@Override
			protected HttpDispatchHandler createHandler(
					ChannelHandlerContext ctx, HttpRequest request) {
				return new NRTRecomputeHandler(ctx, request);
			}
		});

		addUrlMatcher(new UrlMatcher(urlReport) {

			@Override
			protected HttpDispatchHandler createHandler(
					ChannelHandlerContext ctx, HttpRequest request) {
				return new NRTReportHandler(ctx, request);
			}
		});
		addUrlMatcher(new UrlMatcher(urlTest) {

			@Override
			protected HttpDispatchHandler createHandler(
					ChannelHandlerContext ctx, HttpRequest request) {
				return new NRTTestHandler(ctx, request);
			}
		});
	}

}
